package com.example.fw;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactInfo;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactInfo> cachedContacts;
	
	public SortedListOf<ContactInfo> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactInfo>(manager.getHibernateHelper().listContacts());	
	}
	
	public SortedListOf<ContactInfo> getContactsFromUI() {
		SortedListOf<ContactInfo> contactsFromUI = new SortedListOf<ContactInfo>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			String id = row.findElement(By.name("selected[]")).getAttribute("id");
			List<WebElement> cells = row.findElements(By.tagName("td"));
			String lastName = cells.get(1).getText();
			String firstName = cells.get(2).getText();
			String email = cells.get(3).getText();
			String homeNumber = cells.get(4).getText();
			ContactInfo contact = new ContactInfo()
			   .withLastName(lastName)
			   .withFirstName(firstName)
			   .withEmail(email)
			   .withHomeNumber(homeNumber)
			   .withId(id);
			contactsFromUI.add(contact);
		}
		return contactsFromUI;
	}
	
	
	public ContactHelper createContact(ContactInfo contact) {
		manager.navigateTo().mainPage();
	    initContactCreation();
	    fillContactForm(contact, CREATION);
	    submitContactCreation();
	    returnToHomePage();
	    //rebuildCache();
	    return this;
	}
	
	public ContactHelper modifyContact(int index, ContactInfo contact) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		//rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(ContactInfo c, ContactInfo contact) {
		manager.navigateTo().mainPage();
		initContactModification(c);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		//rebuildCache();
		return this;
	}
	
	public void checkFromDB(ContactInfo c) {
		manager.navigateTo().mainPage();
		initContactModification(c);
		
		if (c.getFirstName() == null) {c.setFirstName("");}
		assertEquals(driver.findElement(By.name("firstname")).getAttribute("value"), c.getFirstName());
		
		if (c.getLastName() == null) {c.setLastName("");}
		assertEquals(driver.findElement(By.name("lastname")).getAttribute("value"), c.getLastName());
		
		if (c.getAddress() == null) {c.setAddress("");}
		assertEquals(driver.findElement(By.name("address")).getAttribute("value"), c.getAddress());
		
		if (c.getHomeNumber() == null) {c.setHomeNumber("");}
		assertEquals(driver.findElement(By.name("home")).getAttribute("value"), c.getHomeNumber());
		
		if (c.getMobileNumber() == null) {c.setMobileNumber("");}
		assertEquals(driver.findElement(By.name("mobile")).getAttribute("value"), c.getMobileNumber());
		
		if (c.getWorkNumber() == null) {c.setWorkNumber("");}
		assertEquals(driver.findElement(By.name("work")).getAttribute("value"), c.getWorkNumber());
		
		if (c.getEmail() == null) {c.setEmail("");}
		assertEquals(driver.findElement(By.name("email")).getAttribute("value"), c.getEmail());
		
		if (c.getEmail2() == null) {c.setEmail2("");}
		assertEquals(driver.findElement(By.name("email2")).getAttribute("value"), c.getEmail2());
		
		if (c.getSecAddress() == null) {c.setSecAddress("");}
		assertEquals(driver.findElement(By.name("address2")).getAttribute("value"), c.getSecAddress());
		
		if (c.getSecNumber() == null) {c.setSecNumber("");}
		assertEquals(driver.findElement(By.name("phone2")).getAttribute("value"), c.getSecNumber());
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
	    initContactModification(index);
		submitContactRemoval();
		returnToHomePage();
		//rebuildCache();
		return this;
	}
	
	public ContactHelper deleteContact(ContactInfo contact) {
		manager.navigateTo().mainPage();
		initContactModification(contact);
		submitContactRemoval();
		returnToHomePage();
		//rebuildCache();
		return this;	
	}
	
	
	//------------------------------------------------------------------------------------------------
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactInfo contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomeNumber());
		type(By.name("mobile"), contact.getMobileNumber());
		type(By.name("work"), contact.getWorkNumber());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBday());
	    selectByText(By.name("bmonth"), contact.getBmonth());
	    type(By.name("byear"), contact.getByear());
	    
	    /*if (formType == CREATION) {
	    selectByText(By.name("new_group"), contact.getContactGroup());
	    } else {
	      if (driver.findElements(By.name("new_group")).size() !=0) {
	    	 throw new Error("Group selector exisits in contatc modification form"); 
	      }
	    }*/
	    
	    type(By.name("address2"), contact.getSecAddress());
	    type(By.name("phone2"), contact.getSecNumber());
	    return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		//click(By.cssSelector("img[alt=\"Edit\"]"));
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		return this;
	}
	
	public ContactHelper initContactModification(ContactInfo contact) {
		click(By.xpath("//input[@id='id" + contact.getId() + "']/../../td[7]/a"));
		//input[@id='id141']/../../td[7]
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));	
		cachedContacts = null;
		return this;
	}

	public ContactHelper submitContactRemoval() {
		click(By.xpath("(//input[@name='update'])[2]"));
		cachedContacts = null;
		return this;
	}

}
